package com.creative.accountsdk;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.Toast;
import java.util.HashMap;
import cn.sharesdk.facebook.Facebook;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;

/**
 * Created by yinjialiang on 15-4-2.
 */
public class ThirdLoginActivity extends FragmentActivity implements Handler.Callback{
    private AuthorizeListener authorizeListener;
    private Handler handler;
    private static final int MSG_AUTH_CANCEL = 2;
    private static final int MSG_AUTH_ERROR= 3;
    private static final int MSG_AUTH_COMPLETE = 4;

    private final static String TAG = ThirdLoginActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handler = new Handler(this);
        ShareSDK.initSDK(this);
    }

    public void weiboLogin(AuthorizeListener authorizeListener)
    {
        this.authorizeListener = authorizeListener;
        Platform sina = ShareSDK.getPlatform(SinaWeibo.NAME);
        Log.d(TAG, "authorize weibo");
        authorize(sina);
    }

    public void qqLogin(AuthorizeListener authorizeListener)
    {
        this.authorizeListener = authorizeListener;
        Platform qq = ShareSDK.getPlatform(QQ.NAME);
        Log.d(TAG, "authorize qq");
        authorize(qq);
    }

    public void weixinLogin(AuthorizeListener authorizeListener)
    {
        this.authorizeListener = authorizeListener;
        Platform weixin = ShareSDK.getPlatform(Wechat.NAME);
        Log.d(TAG, "authorize weixin");
        authorize(weixin);
    }

    public void facebookLogin(AuthorizeListener authorizeListener)
    {
        this.authorizeListener = authorizeListener;
        Platform facebook = ShareSDK.getPlatform(Facebook.NAME);
        Log.d(TAG, "authorize facebook");
        authorize(facebook);
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch(msg.what) {
            case MSG_AUTH_CANCEL: {
                //取消授权
                Toast.makeText(this, R.string.auth_cancel, Toast.LENGTH_SHORT).show();
                authorizeListener.authorizeCancel();
            } break;
            case MSG_AUTH_ERROR: {
                //授权失败
                authorizeListener.authorizeError();
                Toast.makeText(this, R.string.auth_error, Toast.LENGTH_SHORT).show();
            } break;
            case MSG_AUTH_COMPLETE: {
                //授权成功
                Toast.makeText(this, R.string.auth_complete, Toast.LENGTH_SHORT).show();
                Object[] objs = (Object[]) msg.obj;
                String platformName = (String) objs[0];
                Platform platform = ShareSDK.getPlatform(platformName);
                int platformID = UserData.ACCOUNT_TYPE_QQ;
                if(platformName.equals(Wechat.NAME))
                {
                    platformID = UserData.ACCOUNT_TYPE_WEIXIN;
                }
                else if(platformName.equals(SinaWeibo.NAME))
                {
                    platformID = UserData.ACCOUNT_TYPE_WEIBO;
                }
                else if(platformName.equals(Facebook.NAME))
                {
                    platformID = UserData.ACCOUNT_TYPE_FACEBOOK;
                }
                authorizeListener.authorizeSuccess(platformID, platform);
            } break;
        }
        return false;
    }

    private void authorize(Platform plat)
    {
        plat.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int action, HashMap<String, Object> res) {
                Log.d(TAG, "onComplete");
                if (action == Platform.ACTION_USER_INFOR) {
                    Message msg = new Message();
                    msg.what = MSG_AUTH_COMPLETE;
                    msg.obj = new Object[]{platform.getName(), res};
                    handler.sendMessage(msg);
                }
            }

            @Override
            public void onError(Platform platform, int action, Throwable t) {
                Log.d(TAG, "onError");
                if (action == Platform.ACTION_USER_INFOR) {
                    handler.sendEmptyMessage(MSG_AUTH_ERROR);
                }
                t.printStackTrace();
            }

            @Override
            public void onCancel(Platform platform, int action) {
                Log.d(TAG, "onCancel");
                if (action == Platform.ACTION_USER_INFOR) {
                    handler.sendEmptyMessage(MSG_AUTH_CANCEL);
                }
            }
        });
        //关闭SSO授权  plat.SSOSetting(true);
        //plat.SSOSetting(true);
        plat.showUser(null);
    }
}
